<?php

class Store_CartController extends YouNet_Controller_Action
{
    private $_user_pages;
    private $_id_page;
    private $_id_store;
    private $_search_params;
    private $_extra_values;


    public function init() {
        parent::init();
        $this->_helper->layout->setLayout('layout_' . $this->_social->getName());
    }
    
    protected function _getSearchParams()
    {
        $this->_search_params['status'] = $this->_getParam('status', null);
        $date_from = $this->_getParam('date_from');
        if(empty($date_from)){
            $this->_search_params['date_from'] = null;
        } else{
            $this->_search_params['date_from'] = $date_from;
        }
        $date_to = $this->_getParam('date_to');
        if(empty($date_to)){
            $this->_search_params['date_to'] = null;
        } else {
            $this->_search_params['date_to'] = $date_to;
        }
    }

    protected function _getExtraValues()
    {
        $this->_extra_values['params']  = $this->_params;
        $this->_extra_values['id_page'] = $this->_id_page;
        $this->_extra_values['id_store'] = $this->_id_store;
        $this->_extra_values['search_params'] = $this->_search_params;
        $this->view->extra_values = $this->_extra_values;
    }
    
    private function _checkAdminPage($id_page)
    {
        foreach ($this->_user_pages as $user_page)
        {
            if ($user_page['id'] == $id_page)
            {
                return true;
            }
        }
        return false;
    }

    public function preDispatch()
	{
		parent::preDispatch();
        $this->_id_page		=	$this->_getParam('id_page', null);
        $this->_id_store		=	$this->_getParam('id_store', null);
        $this->_store = Core_Model_DbTable_Stores::getStoreByUser($this->_id_store, $this->_user);
		$this->view->store = $this->_store;
        $this->_getSearchParams();
        
        $this->_user_pages 	= 	$this->_social->getUserPages();
        $this->view->new_orders = $this->_store->getNumberNewOrders();
        
        $this->view->pages = $this->_user_pages;
        $this->_getExtraValues();
        $this->view->render('store/select_fanpages.phtml');
	}
    
    public function listAction()
    {
    	$form_search = new Store_Form_SearchOrder();
        $form_search->populate($this->_params);
        $this->view->form_search = $form_search;
        
        $page = $this->_getParam('page', 1);
        
        $paginator = Zend_Paginator::factory(Core_Model_DbTable_Carts::getInstance()->getAllCarts($this->_params));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage(ITEM_COUNT_PER_PAGE);
        $this->view->paginator = $paginator;
        
        $this->view->error_messages = $this->_helper->flashMessenger->getMessages();
    }

    public function detailAction()
    {
        $result = Core_Model_DbTable_Carts::getInstance()->markReadForCart($this->_params);
        if(!empty($result)){
            $this->view->new_orders--;
        }
        $cart = Core_Model_DbTable_Carts::getInstance()->getCart($this->_params);
        if(!empty($cart)){
            $list_products = Core_Model_DbTable_CartProducts::getInstance()->getAllProductsInCart($cart->id);
            
            $form_note = new Store_Form_Note();
            $form_note->populate(array('note' => $cart->note));
            
            $this->view->list_products = $list_products;
            $this->view->form_note = $form_note;
            $this->view->cart = $cart;
            $this->view->error_messages = $this->_helper->flashMessenger->getMessages();
        }
        else
        {
            YouNet_Util::redirect($this->view->url(array('action' => 'list', 'id_carts' => null, 'back_action' => null)));
        }
    }
    
    public function markAction()
    {
        $this->disableAutoRender();
        $result = Core_Model_DbTable_Carts::getInstance()->updateStatusListCarts($this->_params);
        if(isset($result))
        {
            if($this->_params['to_status'] == Core_Model_DbTable_Carts::STATUS_DELETE){
                $this->_helper->flashMessenger->addMessage('MSG_DELETE_SUCCESSFUL');
            } else {
                $this->_helper->flashMessenger->addMessage(
                    sprintf(YouNet_Util::translate('MSG_MARK_SUCCESSFUL'), Core_Model_DbTable_Carts::getLowerStatus($this->_params['to_status']))
                );
            }
        }
        else
        {
            $this->_helper->flashMessenger->addMessage('MSG_UPDATE_FAIL');
        }
        if(!empty($this->_params['back_action']))
        {
            YouNet_Util::redirect($this->view->url(array('action' => $this->_params['back_action'], 'id_carts' => null, 'back_action' => null)));
        } else {
            YouNet_Util::redirect($this->view->url(array('action' => 'list', 'id_carts' => null, 'back_action' => null)));
        }
    }
    
    public function importantAction()
    {
        $this->disableAutoRender();
        $result = Core_Model_DbTable_Carts::getInstance()->updateStatusImportant($this->_params);
        if(isset($result))
        {
            $respond = array('success' => true);
            if($this->_params['is_important'] == Core_Model_DbTable_Carts::STATUS_IMPORTANT)
            {
                $respond['is_important'] = true;
                $respond['rel'] = $this->view->url(array('is_important' => Core_Model_DbTable_Carts::STATUS_UNIMPORTANT));
                $respond['message'] = $this->translate('MSG_UPDATE_IMPORTANT_SUCCESSFUL');
            }
            else
            {
                $respond['is_important'] = false;
                $respond['rel'] = $this->view->url(array('is_important' => Core_Model_DbTable_Carts::STATUS_IMPORTANT));
                $respond['message'] = $this->translate('MSG_UPDATE_UNIMPORTANT_SUCCESSFUL');
            }
        }
        else
        {
            $respond = array('success' => false, 'message' => $this->translate('MSG_UPDATE_FAIL'));
        }
        echo json_encode($respond);
    }
    
    public function noteAction()
    {
        $this->disableAutoRender();
        $result = Core_Model_DbTable_Carts::getInstance()->addNote($this->_params);
        if(isset($result))
        {
            $this->_helper->flashMessenger->addMessage('MSG_UPDATE_NOTE_SUCCESSFUL');
        }
        else
        {
            $this->_helper->flashMessenger->addMessage('MSG_UPDATE_FAIL');
        }
        YouNet_Util::redirect($this->view->url(array('action' => 'detail')));
    }
}